//
//  CXComposeButton.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/25.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit

class CXComposeButton: UIButton {

    //关闭高亮效果
    override var highlighted: Bool {
        get {
            return false
        }
        set {}
    }
    /// 传入模型 即可设置
    var buttonInfo: CXComposeModel? {
        didSet {
            setTitle(buttonInfo!.title, forState: .Normal)
            setImage(UIImage(named: buttonInfo!.icon!), forState: .Normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //设置文字和图片的属性
        imageView?.contentMode = .Center //让图片不至于被压扁
        titleLabel?.textAlignment = NSTextAlignment.Center
        setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        titleLabel?.font = UIFont.systemFontOfSize(16)
    }

    //布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        ///  分别设置标签和图片框的位置与尺寸
        imageView?.frame.origin = CGPointZero
        imageView?.frame.size = CGSize(width: self.bounds.width, height: self.bounds.width)
        
        titleLabel?.frame.origin = CGPoint(x: 0, y: self.bounds.width)
        titleLabel?.frame.size = CGSize(width: self.bounds.width, height: self.bounds.height - self.bounds.width)
    }
}
